package Chapter6.dao;

import java.util.List;

import learnning.dao.base.HibernateDao;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import Chapter6.namequeries.BookNameQueries;

public class BookNameQueryDao extends HibernateDao {
	public void add(String name, String address) {
		try {
			begin();
			BookNameQueries bookNameQueries = new BookNameQueries(name, address);
			getSession().save(bookNameQueries);
			commit();
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public BookNameQueries getBook(String name) {
		try {
			begin();
			Query query = getSession().getNamedQuery("findBook");
			query.setParameter("name", name);
			List<BookNameQueries> list = query.list();
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
		}
		return null;
	}

/*	@SuppressWarnings("unchecked")
	public BookNameQueries getBookById(long id) {
		try {
			begin();
			Query query = getSession().getNamedQuery("findBookById");
			query.setParameter("id", id);
			List<BookNameQueries> list = query.list();
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
		}
		return null;
	}*/
}
